package com.jwetherell.my.problems.misc;

/**
 * Implement a circular queue of integers of user-specified size using a simple
 * array. Provide routines to initialize(), enqueue() and dequeue() the queue.
 * Make it thread safe.
 * 
 */
public class CircularQueue {

	private int[] queue;
	private int size = 0;
	private int head = 0;
	private int tail = 0;
	
	public CircularQueue(int length) {
		
		if (length < 0)
			throw new IllegalArgumentException("Legnth should be >= 0");
		
		this.queue = new int[length];
	}

	public void initialize(int[] d) {
		if (d.length > this.queue.length)
			for (int i = 0; i < this.queue.length; i++)
				this.queue[i] = d[i];
		else
			this.queue = d;
	}

	public int deQueue() {
		if (isEmpty())
			return -1;
		
		int i = queue[head];
		increementHead();
		
		return i;
			
	}

	private void increementHead() {
		if (this.head == this.queue.length-1)
			this.head = 0;
		else
			this.head++;
	}

	public void enQueue(int data) {
		if (isFull())
			return;
		
		increementTail();
		
		this.queue[tail] = data;
	}

	private void increementTail() {
		if (this.tail == this.queue.length-1)
			this.tail = 0;
		else
			this.tail++;
	}

	private boolean isEmpty() {
		return head==tail ? true : false;
	}

	private boolean isFull() {
		// TODO Auto-generated method stub
		return false;
	}
}